<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加用药</title>
    <META HTTP-EQUIV="pragma" CONTENT="no-cache">
    <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
    <META HTTP-EQUIV="expires" CONTENT="0">
    <link rel="stylesheet" href="../../../layui/css/layui.css" media="all">
    <script src="../../../js/jquery/3.5.1/jquery.js"></script>
    <script src="../../../layui/layui.js" charset="utf-8"></script>
    <script src="../../../js/config.js"></script>
    <script src="../../../js/function.js"></script>
    <link rel="stylesheet" href="../../../css/public.css">
    <link rel="stylesheet" href="../../../css/patient/patient_info.css">
    <style>
        .baseinfo-box .layui-form-label{width:100px;}
        .layui-form-select dl dd.layui-disabled{color:#000!important;font-weight: bold;}
    </style>
</head>
<body>
<div class="container">
    <div class="add-container2">
        <div class="baseinfo-box" style="width:97%;padding:10px;margin:0 auto;box-sizing: border-box;">
            <form class="layui-form">
                <div class="baseinfo-item">
                    <div class="layui-form-item layui-inline">
                        <div class="layui-form-label">药品名称</div>
                        <div class="layui-input-inline" style="width: 270px">
                            <select name="drug_name" id="drugName" lay-verify="drug_name" lay-verType="tips" lay-filter="drug1" disabled>
                                <option value="0">选择药物</option>
                            </select>
                        </div>
                    </div>
                    <div class="clear"></div>
                </div>
                <div class="baseinfo-item">
                    <div class="layui-form-item layui-inline">
                        <div class="layui-form-label">药物编号</div>
                        <div class="layui-input-inline">
                            <input type="text" name="drug_no" id="drugNo" class="layui-input" placeholder="" readonly style="color:#9999;">
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <div class="layui-form-label" style="letter-spacing: 25px;text-align: center;">药类</div>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input" name="type_name" id="typeName" readonly style="color:#999;">
                        </div>
                    </div>
                    <div class="clear"></div>
                </div>
                <div class="baseinfo-item hide" id="drugAliasBox">
                    <div class="layui-form-item layui-inline">
                        <div class="layui-form-label">药物别名</div>
                        <div class="layui-input-inline">
                            <input type="text" name="drug_alias" id="drugAlias" class="layui-input" placeholder="输入药物别名">
                        </div>
                    </div>
                    <div class="clear"></div>
                </div>
                <div class="baseinfo-item">
                    <div class="layui-form-item layui-inline">
                        <div class="layui-form-label">每日总剂量</div>
                        <div class="layui-input-inline">
                            <input type="text" name="drug_dose" class="layui-input" lay-verify="drug_dose" lay-verType="tips" placeholder="" id="drugDose">
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <div class="layui-form-label" style="letter-spacing: 25px;text-align: center;">单位</div>
                        <div class="layui-input-inline">
                            <select name="unit" id="unit">
                                <option value="ug">ug</option>
                                <option value="mg">mg</option>
                                <option value="g">g</option>
                                <option value="U">U</option>
                                <option value="ml">ml</option>
                                <option value="粒">粒</option>
                                <option value="袋">袋</option>
                                <option value="片">片</option>
                                <option value="支">支</option>
                            </select>
                        </div>
                    </div>
                    <div class="clear"></div>
                </div>
                <div class="baseinfo-item">
                    <div class="layui-form-item layui-inline">
                        <div class="layui-form-label">用法用量</div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <div class="layui-form-label" style="letter-spacing: 25px;width:40px;text-align: center;">早</div>
                        <div class="layui-input-inline" style="width:60px;">
                            <input type="text" class="layui-input" lay-verify="drug_dose" lay-verType="tips" name="usage_dosage_morning">
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <div class="layui-form-label" style="letter-spacing: 25px;width:40px;text-align: center;">中</div>
                        <div class="layui-input-inline" style="width:60px;">
                            <input type="text" class="layui-input" lay-verify="drug_dose" lay-verType="tips" name="usage_dosage_noon">
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <div class="layui-form-label" style="letter-spacing: 25px;width:40px;text-align: center;">晚</div>
                        <div class="layui-input-inline" style="width:60px;">
                            <input type="text" class="layui-input" lay-verify="drug_dose" lay-verType="tips" name="usage_dosage_night">
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <div class="layui-form-label" style="letter-spacing:5px;width:80px;text-align: center;">睡前</div>
                        <div class="layui-input-inline" style="width:60px;">
                            <input type="text" class="layui-input" lay-verify="drug_dose" lay-verType="tips" name="usage_dosage_bedtime">
                        </div>
                    </div>
                    <div class="layui-form-item layui-input-block">
                        <div class="layui-form-label" style="letter-spacing:5px;width:40px;text-align: left;">其他</div>
                        <div class="layui-input-inline" style="width:100px;">
                            <input type="text" class="layui-input" name="other_mock" lay-verify="other_mock" lay-verType="tips" placeholder="填写时间段">
                        </div>
                        <div class="layui-input-inline" style="width:100px;">
                            <input type="text" class="layui-input" name="other_dose" lay-verify="other_dose" lay-verType="tips" placeholder="填写剂量">
                        </div>
                    </div>
                    <div class="clear"></div>
                </div>
                <div class="baseinfo-item hide" id="ydsCiShuBox">
                    <div class="layui-form-item layui-inline">
                        <div class="layui-form-label">注射次数</div>
                        <div class="layui-input-inline">
                            <input type="text"class="layui-input" style="width: 100px" lay-verify="ydsCiShu" lay-verType="tips" id="ydsCishu" name="drug_times"/>
                            如果使用胰岛素泵，注射次数填99，并且在备注栏里写上胰岛素泵
                        </div>
                    </div>
                </div>
                <div class="baseinfo-item">
                    <div class="layui-form-item layui-inline">
                        <div class="layui-form-label">调整日期</div>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input" name="adjust_date" id="adjustDate" lay-verify="date_start" lay-verType="tips" autocomplete="off">
                        </div>
                    </div>
                    <div class="clear"></div>
                </div>

                <div class="baseinfo-item">
                    <div class="layui-form-item layui-inline">
                        <div class="layui-form-label">备注</div>
                        <div class="layui-input-inline" style="width:300px;">
                            <textarea placeholder="输入备注" class="layui-input layui-textarea" name="remark" id="remark"></textarea>
                        </div>
                    </div>

                    <div class="clear"></div>
                </div>
                <div class="baseinfo-item-btn" style="margin:0;">
                    <input name="drug_id" value="0" id="drugID" type="hidden">
                    <button type="button" id="btnClose" style="color:#4777e7;background:#fff;border:1px solid #4777e7;" >取消剂量调整</button>
                    <button type="button" lay-submit="" lay-filter="submit-btn" data-flag="close">保存剂量用药</button>
                </div>
            </form>
        </div>
    </div>
</div>
</body>
<script>
    var modeId = getQueryString('mode_id'); //1 垂体用药 2降糖 3降压 4 其它用药
    var drugId = getQueryString('id'); //1 垂体用药 2降糖 3降压 4 其它用药
    var patient_id = getLocalStorage('patient_id');
    var currDrugs = [];
    var pageIndex = 0;
    var typeId = 0;
    function getDrugInfo(id,form) {
        ajax_get(configUrl.host + '/api/v1/pituitary/drug_detail/'+ id,{id:id},function(res){
            console.log(res);
            if(res.status != 0){
                alert(res.msg);
                return false;
            }
            data = res.data;
            if(data.usage_dosage_morning == 0) data.usage_dosage_morning = '';
            if(data.usage_dosage_noon == 0) data.usage_dosage_noon = '';
            if(data.usage_dosage_night == 0) data.usage_dosage_night = '';
            if(data.usage_dosage_bedtime == 0) data.usage_dosage_bedtime = '';
            if(data.usage_dosage_other.mock == 0) data.usage_dosage_other.mock = '';
            initDrugSelect(form,data.drug_id)
            $("#drugNo").val(data.drug_no);
            $("#typeName").val(data.type_name);
            $("#drugDose").val(data.drug_dose);
            $("#start_date").val(data.start_date);
            $("#end_date").val(data.end_date);
            $("input[name='usage_dosage_morning']").val(data.usage_dosage_morning); //早
            $("input[name='usage_dosage_noon']").val(data.usage_dosage_noon); //中
            $("input[name='usage_dosage_night']").val(data.usage_dosage_night); //晚
            $("input[name='usage_dosage_bedtime']").val(data.usage_dosage_bedtime); //睡前
            $("input[name='other_mock']").val(data.usage_dosage_other.text); //其它时间
            $("input[name='other_dose']").val(data.usage_dosage_other.mock); //其它济量
            //$("#remark").val(data.remark);
            $("#unit").val(data.unit);
            //药物别名
            $("input[name='drug_alias']").val(data.drug_alias);

            if(data.drug_id == (201 + parseInt(modeId))){
                $("#drugAliasBox").show();
            }
            typeId = data.type_id;
            if(data.type_id >= 30 && data.type_id <= 38){
                $("#ydsCiShuBox").show();
                $("#ydsCishu").val(data.drug_times);
            }else{
                $("#ydsCiShuBox").hide();
                $("#ydsCishu").val(0);
            }

        })
    }
    function initDrugSelect(form,drug_id){
        ajax_get(configUrl.getDrugs,{user_id:patient_id,mode:modeId},(res)=> {
            console.log(res);
            if(res.status == 0){
                $.each(res.data.list[modeId],function(index,pItem){
                    //console.log(pItem);
                    str = '<option value="" disabled>'+ pItem.name +'</option>';
                    $("#drugName").append(str);
                    $.each(pItem.children,function(index2,item){
                        currDrugs[item.id] = item;
                        if(drug_id == item.id){
                            selected = ' selected';
                        }else{
                            selected = '';
                        }
                        str = '<option value="'+ item.id +'"'+ selected +'>'+ item.general_name +'</option>';
                        $("#drugName").append(str);
                    })
                    form.render();
                });
            }
            $("#drugID").val(drugId);
            form.render();
        });
    }

    $(document).ready(function () {
        var height = $(window).height() - 2;
        $('.container').height(height);
        $('.add-container').height(height - 51);
        $('.result-box').height(height - 151);
        pageIndex = parent.layui.layer.getFrameIndex(window.name); //获取窗口索引
        console.log('pageIndex',pageIndex);
        layui.use(['element', 'form', 'layer', 'laydate', 'laypage'], function () {
            var element = layui.element;
            var form = layui.form;
            var layer = layui.layer;
            var laydate = layui.laydate;
            var laypage = layui.laypage;
            //调整日期
            laydate.render({
                trigger: 'click',
                elem: '#adjustDate'
            });
            laydate.render({
                trigger: 'click',
                elem: '#end_date'
            });

            getDrugInfo(drugId,form);
            //取消新增
            $("#btnClose").click(function(){
                parent.layui.layer.close(pageIndex);
            });

            //药物切换
            form.on('select(drug1)',function(data){
                var drugNo = currDrugs[data.value]['drug_no'];
                var typeName = currDrugs[data.value]['type_name'];

                $("#drugNo").val(drugNo);
                $("#typeName").val(typeName);
                $("#drugID").val(data.value);

                if(data.value == (201 + parseInt(modeId))){
                    $("#drugAliasBox").show();
                }else{
                    $("#drugAliasBox").hide();
                }
            });

            // v1.1 信息校验
            form.verify({
                drug_name: function(value){
                    if(value == 0){
                        return '请选择药品';
                    }
                },
                drug_dose: function(value){
                    if(!new RegExp("(^$)|^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$").test(value)){
                        return '剂量只能输入正数';
                    }
                    if(parseInt(value) >= 100000){
                        return '最多只能输入5位数字';
                    }
                },
                other_mock: function(value){
                    if(value.length == 0 && $('input[name=other_dose]').val().length > 0){
                        return '请填写时间';
                    }
                    if(value.length > 5){
                        return '只能输入5个字';
                    }
                },
                other_dose: function(value){
                    if(value.length == 0 && $('input[name=other_mock]').val().length > 0){
                        return '请填写剂量';
                    }
                    if(!new RegExp("(^$)|^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$").test(value)){
                        return '剂量只能输入正数';
                    }
                    if(parseInt(value) >= 100000){
                        return '最多只能输入5位数字';
                    }
                },
                ydsCiShu: function(value){
                    if($("#ydsCiShuBox").is(':visible') && value.length == 0){
                        return '请填写注射次数';
                    }
                    if($("#ydsCiShuBox").is(':visible') && !new RegExp("(^$)|^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$").test(value)){
                        return '注射次数只能输入正数';
                    }
                },
                date_start: function(value){
                    if(value.length == 0){
                        return '请输入调整日期';
                    }
                },
                date_end: function(value){
                    var is_unused =  $("#drug_used").prop("checked");
                    if( is_unused == true && value.length == 0){
                        return '请输入结束日期';
                    }
                },
                remark_text: function(value){
                    if(value.length > 50){
                        return '输入内容不能超过50个字';
                    }
                },
            });

            form.on('submit(submit-btn)', function (data) {
                var params = data.field;
                var btnFlag = $(this).attr('data-flag');
                params.user_id = patient_id;
                params.type_id = typeId;
                params.is_used = 0; //是否用过
                params.op_id = getLocalStorage('doc_id'); //操作医生ID
                params._method = 'PUT';
                console.log(params);
                //校验证数据
                // if(params.drug_id == 0){
                //     layer.msg('请选择药品');
                //     return false;
                // }
                // if(params.drug_dose == ''){
                //     layer.msg('请选择总剂量');
                //     return false;
                // }
                // if(params.adjust_date == ''){
                //     layer.msg('请选择调整日期');
                //     return false;
                // }

                //检查总济量是否等于其它济量之和
                allDose = (params.drug_dose == '') ? 0 : parseFloat(params.drug_dose);
                dose1 = (params.usage_dosage_morning == '') ? 0 : parseFloat(params.usage_dosage_morning);
                dose2 = (params.usage_dosage_noon == '') ? 0 :parseFloat(params.usage_dosage_noon);
                dose3 = (params.usage_dosage_night == '') ? 0 :parseFloat(params.usage_dosage_night);
                dose4 = (params.usage_dosage_bedtime == '') ? 0 :parseFloat(params.usage_dosage_bedtime);
                dose5 = (params.other_dose == '') ? 0 :parseFloat(params.other_dose);
                console.log(allDose + ';'+ dose1+';'+ dose2 + ';'+ dose3 + ';'+ dose4 + ';'+ dose5);


                //v1.1 检查输入剂量必须正数
                // if(allDose < 0 || dose1 < 0 || dose2 < 0 || dose3 < 0 || dose4 < 0 || dose5 < 0 ){
                //     layer.msg('药品剂量只能填写正数');
                //     return false;
                // }
                // v1.1 其他字段必须一起填
                // if((params.other_mock.trim().length == 0) != (dose5 == 0)){
                //     layer.msg('其他用量的时间段和剂量必须一起填写');
                //     return false;
                // }

                if(allDose != 0){
                    if((dose1 != 0 || dose2 != 0 || dose3 != 0 || dose4 != 0 || dose5 != 0 ) && allDose != (dose1+dose2+dose3+dose4+dose5)){
                        layer.msg('总剂量和用法用量的和不相等');
                        return false;
                    }
                }else{
                    allDose = dose1+dose2+dose3+dose4+dose5;
                    if(allDose == 0){
                        layer.msg('请输入剂量');
                        return false;
                    }
                    params.drug_dose = allDose;
                }

                //其它用药
                if(params.other_dose != '' && parseFloat(params.other_dose) > 0){
                    var tmp = {
                        'text':params.other_mock,
                        'mock':params.other_dose
                    };
                    params.usage_dosage_other = tmp;
                    //params.usage_dosage_other.text = params.other_mock;
                    //params.usage_dosage_other.mock = params.other_dose;
                    delete(params.other_mock);
                    delete(params.other_dose);
                }
                console.log(params);
                var loadindex = layer.load();
                ajax_post2(configUrl.host + '/api/v1/pituitary/drug_adjust/'+ drugId,params,(res)=>{
                    console.log(res);
                    layer.close(loadindex);
                    if(res.status == 0){
                        layer.msg('调整成功',{icon:1,time:1000},function(){
                            parent.getUserDrugList();
                            //window.location.href='./patient_type.html';
                            if(btnFlag == 'close'){
                                parent.layui.layer.close(pageIndex);
                            }else{
                                window.location.reload();
                            }

                        });
                    }else{
                        layer.msg(res.msg,{icon:5,time:1500});
                    }
                })
                return false;
            });

        })
    })
</script>
</html>